package cn.demomaster.quickui.view.pickview;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.demomaster.quickui.R;
import cn.demomaster.quickui.util.StringUtil;

/**
 * @author squirrel桓
 * @date 2018/11/16.
 * description：
 */
public class TimePicker extends QuickPicker implements View.OnClickListener {
    private Button cancelBtn;
    private Button confirmBtn;
    private LoopView hourLoopView;
    private LoopView minuteLoopView;
    private LoopView meridianLoopView;
    private int hourPos = 0;
    private int minutePos = 0;
    private int meridianPos = 0;

    private int topBottomTextColor = 0xffA9A9A9;//array.getColor(styleable.LoopView_topBottomTextColor, -5263441);
    private int centerTextColor = 0xff11ddaf;//array.getColor(styleable.LoopView_centerTextColor, -13553359);
    private int centerLineColor = 0x00000000;//array.getColor(styleable.LoopView_lineColor, -3815995);
    List<String> hourList = new ArrayList();
    List<String> minList = new ArrayList();
    List<String> meridianList = new ArrayList();

    public TimePicker(Context context, TimePicker.Builder builder) {
        super(context,builder);
        this.mListener = builder.listener;
        this.topBottomTextColor = builder.topBottomTextColor;
        this.centerTextColor = builder.centerTextColor;
        this.centerLineColor = builder.centerLineColor;
        this.initView();
    }

    private void initView() {
        //this.contentView = LayoutInflater.from(this.mContext).inflate(R.layout.layout_time_picker, null);
        this.cancelBtn = findViewById(R.id.btn_cancel);
        this.confirmBtn = findViewById(R.id.btn_confirm);
        this.hourLoopView = findViewById(R.id.picker_hour);
        this.minuteLoopView = findViewById(R.id.picker_minute);
        this.meridianLoopView = findViewById(R.id.picker_meridian);
        this.hourLoopView.setTextColor(topBottomTextColor, centerTextColor, centerLineColor);
        this.hourLoopView.setLoopListener(item -> TimePicker.this.hourPos = item);
        this.minuteLoopView.setTextColor(topBottomTextColor, centerTextColor, centerLineColor);
        this.minuteLoopView.setLoopListener(item -> TimePicker.this.minutePos = item);
        this.meridianLoopView.setTextColor(topBottomTextColor, centerTextColor, centerLineColor);
        this.meridianLoopView.setLoopListener(item -> TimePicker.this.meridianPos = item);
        this.initPickerViews();
        this.cancelBtn.setOnClickListener(this);
        this.confirmBtn.setOnClickListener(this);
    }

    private void initPickerViews() {
        this.hourPos = Calendar.getInstance().get(10) - 1;
        this.minutePos = Calendar.getInstance().get(12);
        this.meridianPos = Calendar.getInstance().get(9);

        int j;
        for (j = 1; j <= 12; ++j) {
            this.hourList.add(StringUtil.formatNumberToStr(j, 2));
        }

        for (j = 0; j < 60; ++j) {
            this.minList.add(StringUtil.formatNumberToStr(j, 2));
        }

        this.meridianList.add("AM");
        this.meridianList.add("PM");
        this.hourLoopView.setDataList(this.hourList);
        this.hourLoopView.setInitPosition(this.hourPos);
        this.minuteLoopView.setDataList(this.minList);
        this.minuteLoopView.setInitPosition(this.minutePos);
        this.meridianLoopView.setDataList(this.meridianList);
        this.meridianLoopView.setInitPosition(this.meridianPos);
    }

    public void onClick(View v) {
        dismiss();
    }

    public static class Builder<T extends Builder> extends QuickPicker.Builder<T> {
        private int topBottomTextColor = 0xffA9A9A9;//array.getColor(styleable.LoopView_topBottomTextColor, -5263441);
        private int centerTextColor = 0xff11ddaf;//array.getColor(styleable.LoopView_centerTextColor, -13553359);
        private int centerLineColor = 0x00000000;//array.getColor(styleable.LoopView_lineColor, -3815995);

        public Builder(Context context, QuickPicker.OnPickListener listener) {
            super(context,listener);
            setContentView(R.layout.quick_layout_time_picker);
            setViewFormat("v-v", new String[]{"HH", "mm"});

            this.pickData = new PickData() {
                @Override
                public List<PickDataItem> getDataByTag(String tag, int[] selectIndexs) {
                    if (tag == "HH") {
                        List<PickDataItem> pickDataItemList = new ArrayList<>();
                        int j;
                        for (j = 1; j < 12; ++j) {
                            PickDataItem pickDataItem = new PickDataItem(StringUtil.formatNumberToStr( j, 2));
                            pickDataItemList.add(pickDataItem);
                        }
                        return pickDataItemList;
                    } else if (tag == "mm") {
                        List<PickDataItem> pickDataItemList = new ArrayList<>();
                        for (int i = 0; i < 60; i++) {
                            PickDataItem pickDataItem = new PickDataItem(StringUtil.formatNumberToStr(i , 2));
                            pickDataItemList.add(pickDataItem);
                        }
                        return pickDataItemList;
                    }
                    return null;
                }

                @Override
                public String[] getTags() {
                    return new String[]{"HH", "mm"};
                }

                @Override
                public int[] getDefautIndex() {
                    return new int[]{0, 0};
                }
            };
        }

        public TimePicker.Builder colorContentText(int topBottomTextColor, int centerTextColor, int centerLineColor) {
            this.topBottomTextColor = topBottomTextColor;
            this.centerTextColor = centerTextColor;
            this.centerLineColor = centerLineColor;
            return this;
        }

        public TimePicker create() {
            return new TimePicker(context,this);
        }
    }
}
